﻿using pachon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PachonDemo
{
    public partial class EditForm : Form
    {
        pcDAL pd = new pcDAL();
        Comm com = new Comm();
        //网页内容存储变量
        string str = null;

        Encoding _endcode = Encoding.UTF8;
        string _webId = null;
        public EditForm(string id)
        {
            InitializeComponent();

            PC_INWEB pc_i = pd.GetPC_INWEB().Where(a => a.id == id).FirstOrDefault();
            if (pc_i == null)
                return;
            else
            {
                _webId = pc_i.id;

                txtWebName.Text = pc_i.webname;
                txtWebUrl.Text = pc_i.weburl;
                txtinWebUrl.Text = pc_i.inweburl;
                txtwebregex1.Text = pc_i.webregex1;
                txtwebregex2.Text = pc_i.webregex2;
                txtwebregex3.Text = pc_i.webregex3;
                if (pc_i.cando == "1") cbcando.Checked = true; else cbcando.Checked = false;
                txtendcode.Text = pc_i.endcode;
                txtcity.Text = pc_i.pccity;
                txttype.Text = pc_i.pctype;
                txtappendurl.Text = pc_i.appendurl;
                labtcreatertime.Text = pc_i.creatertime + "";
                labtuddatetime.Text = pc_i.uddatetime + "";

                if (pc_i.endcode == "gb2312")
                {
                    str = com.GetUrlContent(txtinWebUrl.Text, Encoding.Default);
                    _endcode = Encoding.Default;
                }
                else
                {
                    str = com.GetUrlContent(txtinWebUrl.Text, Encoding.UTF8);
                    _endcode = Encoding.UTF8;
                }
                PC_CONTENT_REGEX pc_c_r = pd.GetPC_CONTENT_REGEX().Where(a => a.weburl_id == pc_i.id).FirstOrDefault();
                if (pc_c_r != null)
                {
                    txttitle_regex1.Text = pc_c_r.title_regex1;
                    txttitle_regex2.Text = pc_c_r.title_regex2;
                    txttitle_regex3.Text = pc_c_r.title_regex3;

                    txttime_regex1.Text = pc_c_r.time_regex1;
                    txttime_regex2.Text = pc_c_r.time_regex2;
                    txttime_regex3.Text = pc_c_r.time_regex3;

                    txtcontent_regex1.Text = pc_c_r.content_regex1;
                    txtcontent_regex2.Text = pc_c_r.content_regex2;
                    txtcontent_regex3.Text = pc_c_r.content_regex3;
                }
            }
        }
        List<string> uploadDic = null;

        private void butwebregex1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtwebregex1.Text) == true)
                return;
            uploadDic = new List<string>();
            Regex reg = new Regex(txtwebregex1.Text.Trim());
            var mat = reg.Matches(str);
            string strrr = "";
            foreach (Match a in mat)
            {
                var aaaa = a.Groups[0].Value;
                strrr = strrr + aaaa + "\r\n";

                uploadDic.Add(aaaa);
            }
            ShowMsg sm = new ShowMsg(strrr);
            sm.ShowDialog();
        }

        private void butwebregex2_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtwebregex1.Text) == true || string.IsNullOrEmpty(txtwebregex2.Text) == true)
                    return;
                uploadDic = new List<string>();
                Regex reg = new Regex(txtwebregex1.Text);
                //Regex reg2 = new Regex("/\\d[^\\.]+\\.htm");
                Regex reg2 = new Regex(txtwebregex2.Text);
                var mat = reg.Matches(str);
                List<string> mc = new List<string>();
                foreach (Match a in mat)
                {
                    var aaaa = a.Groups[0].Value;
                    var maat = reg2.Match(aaaa) + "";
                    mc.Add(maat);
                }
                string url = txtappendurl.Text;
                string strrr = "";
                foreach (string a in mc)
                {
                    if (string.IsNullOrEmpty(txtwebregex3.Text) == false)
                        strrr = strrr + a + "\r\n";
                    else
                    {
                        strrr = strrr + url + a + "\r\n";
                        uploadDic.Add(url + a);
                    }
                }
                ShowMsg sm = new ShowMsg(strrr);
                sm.ShowDialog();
            }
            catch
            {
                MessageBox.Show("error");
            }
        }

        private void butwebregex3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtwebregex1.Text) == true || string.IsNullOrEmpty(txtwebregex2.Text) == true || string.IsNullOrEmpty(txtwebregex3.Text) == true)
                return;
            try
            {
                if (string.IsNullOrEmpty(txtwebregex1.Text) == true || string.IsNullOrEmpty(txtwebregex2.Text) == true)
                    return;
                uploadDic = new List<string>();
                Regex reg = new Regex(txtwebregex1.Text);
                //Regex reg2 = new Regex("/\\d[^\\.]+\\.htm");
                Regex reg2 = new Regex(txtwebregex2.Text);
                Regex reg3 = new Regex(txtwebregex3.Text);
                var mat = reg.Matches(str);
                List<string> mc = new List<string>();
                foreach (Match a in mat)
                {
                    var aaaa = a.Groups[0].Value;
                    var maat = reg2.Match(aaaa) + "";
                    var maat2 = reg3.Match(maat) + "";
                    mc.Add(maat2);
                }
                string url = txtappendurl.Text;
                string strrr = "";
                foreach (string a in mc)
                {
                    strrr = strrr + url + a + "\r\n";
                    uploadDic.Add(url + a);
                }

                ShowMsg sm = new ShowMsg(strrr);
                sm.ShowDialog();
            }
            catch
            {
                MessageBox.Show("error");
            }
        }
        private void butsaveEdit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_webId) == true)
            {
                return;
            }
            else if (string.IsNullOrEmpty(txtinWebUrl.Text) == true)
            {
                MessageBox.Show("入口网址必填!");
                return;
            }
            else if (string.IsNullOrEmpty(txtendcode.Text) == true)
            {
                MessageBox.Show("网页编码格式必填!");
                return;
            }
            //else if (string.IsNullOrEmpty(txtendcode.Text) == false)
            //{
            //    string ssssssss = txtendcode.Text.ToLower();
            //    if (!(ssssssss.Equals("gb2312") || !ssssssss.Equals("utf-8")))
            //    {
            //        MessageBox.Show("网页编码格式错误!(按照提示来...)");
            //        return;
            //    }
            //}
            else
            {
                PC_INWEB pc_i = new PC_INWEB();

                pc_i.id = _webId;
                pc_i.weburl = txtWebUrl.Text;
                pc_i.inweburl = txtinWebUrl.Text;
                pc_i.webname = txtWebName.Text;
                pc_i.cando = cbcando.Checked == true ? "1" : "0";
                pc_i.endcode = txtendcode.Text;
                pc_i.webregex1 = txtwebregex1.Text;
                pc_i.webregex2 = txtwebregex2.Text;
                pc_i.webregex3 = txtwebregex3.Text;
                pc_i.uddatetime = System.DateTime.Now;

                 pc_i.pccity=txtcity.Text ;
                 pc_i.pctype=txttype.Text ;
                 pc_i.appendurl = txtappendurl.Text;

                int ret_i = pd.UpdataPC_INWEB(pc_i);
                if (ret_i == 1)
                    MessageBox.Show("保存成功...");
                else
                    MessageBox.Show("error...");
            }
        }

        private void butsavetow_Click(object sender, EventArgs e)
        {
            PC_CONTENT_REGEX pc_c_r = new PC_CONTENT_REGEX();
            //pc_c_r. id 

            pc_c_r.weburl_id = _webId;

            pc_c_r.title_regex1 = txttitle_regex1.Text;
            pc_c_r.title_regex2 = txttitle_regex2.Text;
            pc_c_r.title_regex3 = txttitle_regex3.Text;

            pc_c_r.time_regex1 = txttime_regex1.Text;
            pc_c_r.time_regex2 = txttime_regex2.Text;
            pc_c_r.time_regex3 = txttime_regex3.Text;

            pc_c_r.content_regex1 = txtcontent_regex1.Text;
            pc_c_r.content_regex2 = txtcontent_regex2.Text;
            pc_c_r.content_regex3 = txtcontent_regex3.Text;

            int ret_i = pd.UpdatePC_CONTENT_REGEX(pc_c_r);
            if (ret_i == 1)
                MessageBox.Show("保存成功...");
            else
                MessageBox.Show("error...");

            //string s = com.GetFormCode(FormType.pachon);
            //MessageBox.Show(s);
            //MessageBox.Show(com.RandCode(10));
        }

        private void buttitle_regex1_Click(object sender, EventArgs e)
        {
            List<string> lsUrl = com.GetRegexString(str, txtwebregex1.Text, txtwebregex2.Text, txtwebregex3.Text, txtappendurl.Text);
            string urls = lsUrl.FirstOrDefault();
            if (urls == null)
                return;
            else
            {
                string htmlstrrr = com.GetUrlContent(urls, _endcode);
                List<string> strurlhtl = com.GetRegexString(htmlstrrr, txttitle_regex1.Text, txttitle_regex2.Text, txttitle_regex3.Text);
                ShowMsg sm = new ShowMsg(strurlhtl.FirstOrDefault());
                sm.ShowDialog();
            }
        }

        private void buttime_regex1_Click(object sender, EventArgs e)
        {
            List<string> lsUrl = com.GetRegexString(str, txtwebregex1.Text, txtwebregex2.Text, txtwebregex3.Text, txtappendurl.Text);
            string urls = lsUrl.FirstOrDefault();
            if (urls == null)
                return;
            else
            {
                string htmlstrrr = com.GetUrlContent(urls, _endcode);
                List<string> strurlhtl = com.GetRegexString(htmlstrrr, txttime_regex1.Text, txttime_regex2.Text, txttime_regex3.Text);
                ShowMsg sm = new ShowMsg(strurlhtl.FirstOrDefault());
                sm.ShowDialog();
            }
        }

        private void butcontent_regex1_Click(object sender, EventArgs e)
        {
            List<string> lsUrl = com.GetRegexString(str, txtwebregex1.Text, txtwebregex2.Text, txtwebregex3.Text, txtappendurl.Text);
            string urls = lsUrl.FirstOrDefault();
            if (urls == null)
                return;
            else
            {
                string htmlstrrr = com.GetUrlContent(urls, _endcode);
                List<string> strurlhtl = com.GetRegexString(htmlstrrr, txtcontent_regex1.Text, txtcontent_regex2.Text, txtcontent_regex3.Text);
                ShowMsg sm = new ShowMsg(strurlhtl.FirstOrDefault());
                sm.ShowDialog();
            }
        }
    }
}
